package com.wuye.controller;

import com.wuye.dao.UsersDao;
import com.wuye.dao.UsersDaoImpl;
import com.wuye.entity.Users;

import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 控制层
 */
public class UsersServlet extends HttpServlet {

    /**
     * 请求入口，统一入口层
     * @param request
     * @param response
     * @throws javax.servlet.ServletException
     * @throws IOException
     */
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
            response.setContentType("text/html;charset=utf-8");
            request.setCharacterEncoding("utf-8");
           String method=request.getParameter("method"); //获取方法名
           if(method!=null){//为空的方法名不处理

               if(method.equals("login")){//登录逻辑处理
                   UsersDao usersDao=new UsersDaoImpl();
                   String uname=request.getParameter("uname");
                   String upwd=request.getParameter("upwd");
                   List<Object> objects=new ArrayList<>();
                   objects.add(uname);
                   objects.add(upwd);
                   Users users=usersDao.login(objects);
                   if(users!=null){
                       request.getSession().removeAttribute("msg");
                       response.sendRedirect("/UsersServlet?method=list");
                   }else{
                       request.getSession().setAttribute("msg","账号密码错误");
                       request.getRequestDispatcher("/index.jsp").forward(request,response);
                   }
               }

               if(method.equals("list")){//列表处理
                   UsersDao usersDao=new UsersDaoImpl();
                   List<Users> users=usersDao.getUsersAll();
                   request.getSession().setAttribute("users",users);
                   request.getRequestDispatcher("/list.jsp").forward(request,response);
               }

               if(method.equals("register")){//注册
                   String uname=request.getParameter("uname");
                   String usex=request.getParameter("usex");
                   String uwork=request.getParameter("uwork");
                   String uaddress=request.getParameter("uaddress");
                   String uphone=request.getParameter("uphone");
                   String uemail=request.getParameter("uemail");
                   String upwd=request.getParameter("upwd");
                   List<Object> objects=new ArrayList<>();
                   objects.add(uname);
                   objects.add(usex);
                   objects.add(uwork);
                   objects.add(uaddress);
                   objects.add(uphone);
                   objects.add(uemail);
                   objects.add(upwd);
                   UsersDao usersDao=new UsersDaoImpl();
                   usersDao.saveUser(objects);
                   request.getRequestDispatcher("/login.jsp").forward(request,response);
               }


               if(method.equals("edit")){//修改跳转
                   String userid=request.getParameter("userid");
                   UsersDao usersDao=new UsersDaoImpl();
                   Users users=usersDao.getUserId(userid);
                   request.getSession().setAttribute("us",users);
                   request.getRequestDispatcher("/update.jsp").forward(request,response);
               }


               if(method.equals("update")){//修改
                   String userid=request.getParameter("userid");
                   String uname=request.getParameter("uname");
                   String usex=request.getParameter("usex");
                   String uwork=request.getParameter("uwork");
                   String uaddress=request.getParameter("uaddress");
                   String uphone=request.getParameter("uphone");
                   String uemail=request.getParameter("uemail");
                   String upwd=request.getParameter("upwd");
                   List<Object> objects=new ArrayList<>();
                   objects.add(uname);
                   objects.add(usex);
                   objects.add(uwork);
                   objects.add(uaddress);
                   objects.add(uphone);
                   objects.add(uemail);
                   objects.add(upwd);
                   objects.add(userid);
                   UsersDao usersDao=new UsersDaoImpl();
                   usersDao.updateUser(objects);
                   response.sendRedirect("/UsersServlet?method=list");
               }


               if(method.equals("delete")){//删除
                   String userid=request.getParameter("userid");
                   List<Object> objects=new ArrayList<>();
                   objects.add(userid);
                   UsersDao usersDao=new UsersDaoImpl();
                   usersDao.deleteUser(objects);
                   response.sendRedirect("/UsersServlet?method=list");
               }

           }
    }

    /**
     * get请求转发给post请求处理
     * @param request
     * @param response
     * @throws javax.servlet.ServletException
     * @throws IOException
     */
    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
            doPost(request,response);
    }
}
